import process from 'process'
import logging from '../logging'
import * as outputs from '../outputs'
import {uploadReport} from './uploadReport'

const url = 'http://testing.rdc.aliyun.com/assets'


export interface TestResult {
    //local path of the report file
    reportLocalPath: string;
    coverage?: Coverage | undefined;
    coverageRate?: CoverageRate | undefined;
}

export interface Coverage {
    total: number;
    passed: number;
    failed: number;
    skipped: number;
    passedRate: number;
}

export interface CoverageRate {
    lineCoverageRate?: number;
    branchCoverageRate?: number;
    methodCoverageRate?: number;
    classCoverageRate?: number;
    instructionCoverageRate?: number;
}



export async function outputResult(testResult: TestResult){
    enableResult()
    const objectKey = await uploadReport(testResult.reportLocalPath)
    addReportURL(`${url}/${objectKey}`)

    if (testResult.coverage) {
        const coverageEntries = Object.entries(testResult.coverage);
        for (const [key, value] of coverageEntries) {
            const capitalizedKey = capitalizeFirstLetter(key);
            addValButton(capitalizedKey, value, 'test case');
        }
    }
    if (testResult.coverageRate) {
        const coverageRateEntries = Object.entries(testResult.coverageRate);
        for (const [key, value] of coverageRateEntries) {
            if (value !== undefined) {
                const capitalizedKey = capitalizeFirstLetter(key);
                addValButton(capitalizedKey, value, 'coverage');
            }
        }
    }
}

function capitalizeFirstLetter(str: string): string {
    return str.charAt(0).toUpperCase() + str.slice(1);
}

// type: choose from 'test case' , 'issue' and other available types
function addValButton(key:string, val:number, type:string): void {
    const title = `[${process.env['stepIdentifier']}]STAT_TITLE_${key}: ${key}`;
    outputs.addOutput(title)
    logging.info(title)

    const name = `[${process.env['stepIdentifier']}]STAT_NAME_${key}: ${type}`;
    outputs.addOutput(name)
    logging.info(name)

    const value = `[${process.env['stepIdentifier']}]STAT_VALUE_${key}: ${val}`;
    outputs.addOutput(value)
    logging.info(value)

    const style = `[${process.env['stepIdentifier']}]STAT_STYLE_${key}: text-danger`;
    outputs.addOutput(style)
    logging.info(style)
}

function addReportURL(reportUrl:string): void {

    const titleReport = `[${process.env['stepIdentifier']}]STAT_TITLE__REPORT: report`;
    outputs.addOutput(titleReport)
    logging.info(titleReport)

    const valueReport = `[${process.env['stepIdentifier']}]STAT_VALUE__REPORT: link`;
    outputs.addOutput(valueReport)
    logging.info(valueReport)

    const outputReport = `[${process.env['stepIdentifier']}]STAT_URL__REPORT:${reportUrl}`;
    outputs.addOutput(outputReport)
    logging.info(outputReport)
}

function enableResult(): void {
    const outputTitle = `[${process.env['stepIdentifier']}]STAT_INFO_TITLE:${process.env['name']}`;
    outputs.addOutput(outputTitle)
    logging.info(outputTitle)
}
